/**
 * Created by zhangxiaoyu on 2016-09-18.
 */
angular.module('hyt.mailListCtrl',[])

    //通讯录
    .controller('MailListCtrl',function($scope,$ionicLoading,$state,Storage,MailShrFactory,MailFhrFactory,MailSjFactory,$ionicHistory,$stateParams,$ionicPopup,$window,$ionicTabsDelegate){
        $scope.showloading=true;
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var dw_id=Storage.get('dw_id');
        var sflx='';
        var khmc='';
        var lxdh='';
        var pyjm='';
        MailShrFactory.getMailShr(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
        $scope.$on('MailShr.shrUpdated',function(){
            var result=MailShrFactory.getShr();
            $scope.mailListShr=result.data;
            $scope.showloading=false;
        });
        MailFhrFactory.getMailFhr(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
        $scope.$on('MailFhr.fhrUpdated',function(){
            var result=MailFhrFactory.getFhr();
            $scope.mailListFhr=result.data;
            $scope.showloading=false;
        });
        MailSjFactory.getMailSj(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
        $scope.$on('MailSj.sjUpdated',function(){
            var result=MailSjFactory.getSj();
            $scope.mailListSj=result.data;
            console.log($scope.mailListSj);
            $scope.showloading=false;
        });
        //下拉更新
        $scope.doRefreshShr=function(){
            MailShrFactory.getMailShr(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
            $scope.$broadcast('scroll.refreshComplete');
        };
        $scope.doRefreshFhr=function(){
            MailFhrFactory.getMailFhr(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
            $scope.$broadcast('scroll.refreshComplete');
        };
        $scope.doRefreshSj=function(){
            MailSjFactory.getMailSj(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
            $scope.$broadcast('scroll.refreshComplete');
        };
        //上拉更新
        $scope.loadMoreShr=function(){
            MailShrFactory.getMoreShr(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        $scope.loadMoreFhr=function(){
            MailFhrFactory.getMoreFhr(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        $scope.loadMoreSj=function(){
            MailSjFactory.getMoreSj(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        //下一页
        $scope.hasNextPageShr = function() {
            return MailShrFactory.hasNextPage();
        };
        $scope.hasNextPageFhr = function() {
            return MailFhrFactory.hasNextPage();
        };
        $scope.hasNextPageSj = function() {
            return MailSjFactory.hasNextPage();
        };
        $scope.search={
            content:''
        };

        $scope.searchLinkman=function(val,sflx){
            var re = /^[0-9]+.?[0-9]*$/;
            var reg= /^[A-Za-z]+$/;
            if (re.test(val)){
                lxdh=val;
            }else if (reg.test(val)){
                pyjm=val.toUpperCase();
            }else {
                khmc=val;
            }
            if (sflx==1){
                MailShrFactory.getMailShr(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
            }else if (sflx==2){
                MailFhrFactory.getMailFhr(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
            }else if (sflx==3){
                MailSjFactory.getMailSj(sid,user_id,yh_id,dw_id,khmc,lxdh,pyjm);
            }
        };
        //长按删除
        $scope.deleteShr=function(data){
            var receipt = $ionicPopup.confirm({
                title : '是否删除联系人',
                cancelText : '取消',
                cancelType : 'button-stable',
                okText : '删除'
            });
            receipt.then(function(r){
                if(r){
                    MailShrFactory.getDelete(sid,user_id,dw_id,yh_id,data.kh_id);
                    $scope.$on('DeleteData',function(){
                        var result=MailShrFactory.deleteResult();
                        if (result==1){
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "删除成功",
                                duration: 1500
                            });
                            $scope.mailListShr.splice($scope.mailListShr.indexOf(data), 1);
                        }else {
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "删除失败",
                                duration: 1500
                            });
                        }
                    });
                }else{
                    console.log('取消');
                }
            });
        };
        $scope.deleteFhr=function(data){
            var receipt = $ionicPopup.confirm({
                title : '是否删除联系人',
                cancelText : '取消',
                cancelType : 'button-stable',
                okText : '删除'
            });
            receipt.then(function(r){
                if(r){
                    MailShrFactory.getDelete(sid,user_id,dw_id,yh_id,data.kh_id);
                    $scope.$on('DeleteData',function(){
                        var result=MailShrFactory.deleteResult();
                        if (result==1){
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "删除成功",
                                duration: 1500
                            });
                            $scope.mailListFhr.splice($scope.mailListFhr.indexOf(data), 1);
                        }else {
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "删除失败",
                                duration: 1500
                            });
                        }
                    });
                }else{
                    console.log('取消');
                }
            });
        };
        $scope.callPhone = function (mobilePhone) {
            var confirmPopup = $ionicPopup.confirm({
                title:mobilePhone,
                okText:'呼叫',
                cancelText:'取消'
            });
            confirmPopup.then(function(res) {
                if (res) {
                    $window.location.href = "tel:" + mobilePhone;
                }else {

                }
            });

        };
        $scope.dragLeftShr=function(){
            $ionicTabsDelegate.select(1);
        };
        $scope.dragLeftFhr=function(){
            $ionicTabsDelegate.select(2);
        };
        $scope.dragRightFhr=function(){
            $ionicTabsDelegate.select(0);
        };
        $scope.dragLeftSj=function(){
            $ionicTabsDelegate.select(3);
        };
        $scope.dragRightSj=function(){
            $ionicTabsDelegate.select(1);
        };
        $scope.myGoBack = function() {
            $ionicHistory.goBack();
        };
    });
